"""
Adam Prejean
Date Created: 11/20/2014
ap_DisplayToggles.py

How to run:
import ap_DisplayToggles
reload (ap_DisplayToggles)
ap_DisplayToggles.ActivateUI()

"""

import maya.cmds as cmds

window_name = 'ToggleWindow'
width = 235
window_bgc = (0.25,0.25,0.25)
title_color = (0.987342, 0.841056, 0.299952)


# GUI Functions
def ActivateUI():
    if (cmds.window(window_name, q=True, ex=True)):
	    cmds.deleteUI(window_name)	
    #if (cmds.windowPref(window_name, ex=True)):
    #    cmds.windowPref(window_name, r=True)
	    
    window_object = cmds.window(window_name, bgc=window_bgc, w=width, title = "Display Toggles", sizeable=False, mnb=False, mxb=False, toolbox=True)
    gui_creation()
    cmds.showWindow( window_object )
    #window_object.show()
    
    
def gui_creation():
    cmds.columnLayout(w=width)
    main_form = cmds.formLayout(nd=100, w=width)

    row_1 = cmds.rowColumnLayout(nc=3, cw=[(1,75), (2,75), (3, 75)])    
    text_button((0.35,0.35,0.35), 'Outliner', win_outliner, 'Open Outliner Window')
    text_button((0.35,0.35,0.35), 'Hypershade', win_hypershade, 'Open Outliner Window')
    text_button((0.35,0.35,0.35), 'Graph Edit.', win_graphEditor, 'Open Outliner Window')
    text_button((0.35,0.35,0.35), 'MTK', win_modelingToolkit, 'Open Outliner Window')
    text_button((0.35,0.35,0.35), 'Node Edit.', win_nodeEditor, 'Open Outliner Window')
    text_button((0.35,0.35,0.35), 'Script Edit.', win_scriptEditor, 'Open Outliner Window')
    cmds.setParent(main_form)


    row_2 = cmds.rowColumnLayout(nc=7, cw=[(1,32), (2,32), (3, 32), (4, 32), (5,32), (6,32), (7,32)])
    icon_button((0.25,0.25,0.25), 'gridDisplay.png', toggleGrid, 'Toggle Grid')
    icon_button((0.25,0.25,0.25), 'toggleJoints.bmp', toggleJoints, 'Toggle Joints')
    icon_button((0.25,0.25,0.25), 'toggleNurbs.bmp', toggleCurves, 'Toggle NURBS Curves')
    icon_button((0.25,0.25,0.25), 'cube.png', togglePolygons, 'Toggle Polygons')
    icon_button((0.25,0.25,0.25), 'WireFrameOnShaded.png', toggleWireShaded, 'Toggle Wireframe on Shaded')
    icon_button((0.25,0.25,0.25), 'XRay.png', toggleXRay, 'Toggle XRay')
    icon_button((0.263,1,0.639), 'visible.png', toggleWireSelected, 'Toggle Wireframe on Selected')
    cmds.setParent(main_form)



    '''
    row_3 = cmds.rowColumnLayout(nc=4, cw=[(1,35), (2,35), (3, 35), (4, 35)])
    create_button('menuIconWindow.png', 'CE', win_connectionEditor, 'Connection Editor')
    create_button('menuIconWindow.png', 'CE', win_connectionEditor, 'Connection Editor')
    create_button('menuIconWindow.png', 'CE', win_connectionEditor, 'Connection Editor')
    create_button('menuIconKeys.png', 'Set', win_setDrivenKey, 'Set Driven Key')
    cmds.setParent(main_form)
    '''

    cmds.formLayout(main_form, e=True,
	attachForm=[(row_2, 'right', 5), (row_2, 'left', 5), (row_1, 'right', 5), (row_1, 'left', 5), (row_1, 'top', 5)],
	attachControl=[(row_2, 'top', 5, row_1)],
	attachPosition=[(row_1, 'left', 0, 1.5), (row_2, 'left', 0, 1.5)])
    
'''   
    cmds.formLayout(main_form, e=True,
	attachForm=[(row_3, 'bottom', 5), (row_3, 'right', 5), (row_3, 'left', 5), (row_2, 'right', 5), (row_2, 'left', 5), (row_1, 'right', 5), (row_1, 'left', 5), (row_1, 'top', 5)],
	attachControl=[(row_3, 'top', 5, row_2), (row_2, 'top', 5, row_1)],
	attachPosition=[(row_1, 'left', 0, 20), (row_2, 'left', 0, 20), (row_3, 'left', 0, 25)])
'''
    
def icon_button(BGcolor, button_picture, function_call, annotation):
    test_button = cmds.iconTextButton( backgroundColor=BGcolor, image1=button_picture, c=function_call, ann=annotation, w=32, h=32) 


def text_button(BGcolor, button_name, function_call, annotation):
    test_button = cmds.button( backgroundColor=BGcolor, label=button_name, c=function_call, ann=annotation, enableBackground=False, w=70, h=32)


'''
def ActivateUI():
    # Window Creation =================================================================================================
    
    if cmds.window( "ToggleWindow", exists=True, q=True ):
        cmds.deleteUI( "ToggleWindow", window=True )
    
    window = cmds.window( "ToggleWindow", title = "Display Toggles", sizeable=False, mnb=False, mxb=False, toolbox=True)
    form = cmds.formLayout(numberOfDivisions=100)
    
    # Button Creation =================================================================================================
    object = cmds.iconTextButton( image1="gridDisplay.png", c=toggleGrid, ann="Toggle Grid", w=32, h=32)
    cmds.formLayout( form, edit=True, attachForm=[( object, "top", 5), ( object, "left", 5)] )
    #=========================================
    object = cmds.iconTextButton( image1="toggleJoints.bmp", c=toggleJoints, ann="Toggle Joints", w=32, h=32)
    cmds.formLayout( form, edit=True, attachForm=[( object, "top", 40), ( object, "left", 5)] )
    #=========================================
    object = cmds.iconTextButton( image1="toggleNurbs.bmp", c=toggleCurves, ann="Toggle NURBS Curves", w=32, h=32)
    cmds.formLayout( form, edit=True, attachForm=[( object, "top", 75), ( object, "left", 5)] )
    #=========================================
    object = cmds.iconTextButton( image1="cube.png", c=togglePolygons, ann="Toggle Polygons", w=32, h=32)
    cmds.formLayout( form, edit=True, attachForm=[( object, "top", 110), ( object, "left", 5)] )
    #=========================================
    object = cmds.iconTextButton( image1="WireFrameOnShaded.png", c=toggleWireShaded, ann="Toggle Wireframe on Shaded", w=32, h=32)
    cmds.formLayout( form, edit=True, attachForm=[( object, "top", 145), ( object, "left", 5)] )
    #=========================================
    object = cmds.iconTextButton( image1="XRay.png", c=toggleXRay, ann="Toggle XRay", w=32, h=32)
    cmds.formLayout( form, edit=True, attachForm=[( object, "top", 180), ( object, "left", 5)] )
    #=========================================
    object = cmds.iconTextButton( backgroundColor=(0.263,1,0.639), image1="visible.png", c=toggleWireSelected, ann="Toggle Wireframe on Selected", w=32, h=32)
    cmds.formLayout( form, edit=True, attachForm=[( object, "top", 215), ( object, "left", 5)] )
    #=========================================
    #=========================================
    #=========================================
    object = cmds.button( backgroundColor=(0.35,0.35,0.35), c=win_outliner, enableBackground=False, label="Outliner", w=70, h=32)
    cmds.formLayout( form, edit=True, attachForm=[( object, 'top', 5), ( object, 'left', 40)] )
    #=========================================
    object = cmds.button( backgroundColor=(0.35,0.35,0.35), c=win_outliner, enableBackground=False, label="Hypershade", w=70, h=32)
    cmds.formLayout( form, edit=True, attachForm=[( object, 'top', 40), ( object, 'left', 40)] )
    #=========================================
    object = cmds.button( backgroundColor=(0.35,0.35,0.35), c=win_outliner, enableBackground=False, label="UV Editor", w=70, h=32)
    cmds.formLayout( form, edit=True, attachForm=[( object, 'top', 75), ( object, 'left', 40)] )
    #=========================================
    object = cmds.button( backgroundColor=(0.35,0.35,0.35), c=win_outliner, enableBackground=False, label="Connection", w=70, h=32)
    cmds.formLayout( form, edit=True, attachForm=[( object, 'top', 110), ( object, 'left', 40)] )
    #=========================================
    object = cmds.button( backgroundColor=(0.35,0.35,0.35), c=win_outliner, enableBackground=False, label="Node Edit", w=70, h=32)
    cmds.formLayout( form, edit=True, attachForm=[( object, 'top', 145), ( object, 'left', 40)] )
    #=========================================
    object = cmds.button( backgroundColor=(0.35,0.35,0.35), c=win_outliner, enableBackground=False, label="Node Edit", w=70, h=32)
    cmds.formLayout( form, edit=True, attachForm=[( object, 'top', 180), ( object, 'left', 40)] )
    #=========================================
    object = cmds.button( backgroundColor=(0.35,0.35,0.35), c=win_outliner, enableBackground=False, label="Node Edit", w=70, h=32)
    cmds.formLayout( form, edit=True, attachForm=[( object, 'top', 215), ( object, 'left', 40)] )
    #=========================================



    cmds.setParent( ".." )
    
    cmds.window( "ToggleWindow", edit=True, widthHeight=(252, 252)) #For 1 Row Height= 50, For 2 Rows Height= 100,For 3 Rows Height=128
    
    cmds.showWindow( window )

'''
# Work Functions

def toggleCurves():
    modelPanel=cmds.getPanel( withFocus=True )
    if (cmds.modelEditor(modelPanel, query=1, nurbsCurves=1)==1):
        cmds.modelEditor(modelPanel, edit=1, nurbsCurves=0)
    else:
        cmds.modelEditor(modelPanel,edit=1,nurbsCurves=1)

def toggleJoints():
    modelPanel=cmds.getPanel( withFocus=True )
    if (cmds.modelEditor(modelPanel, query=1, joints=1)==1):
        cmds.modelEditor(modelPanel, edit=1, joints=0)
    else:
        cmds.modelEditor(modelPanel,edit=1,joints=1)

def toggleGrid():
    modelPanel=cmds.getPanel( withFocus=True )
    if (cmds.modelEditor(modelPanel, query=1, grid=1)==1):
        cmds.modelEditor(modelPanel, edit=1, grid=0)
    else:
        cmds.modelEditor(modelPanel,edit=1,grid=1)

def togglePolygons():
    modelPanel=cmds.getPanel( withFocus=True )
    if (cmds.modelEditor(modelPanel, query=1, polymeshes=1)==1):
        cmds.modelEditor(modelPanel, edit=1, polymeshes=0)
    else:
        cmds.modelEditor(modelPanel,edit=1,polymeshes=1)

def toggleWireShaded():
    modelPanel=cmds.getPanel( withFocus=True )
    if (cmds.modelEditor(modelPanel, query=1, wos=1)==1):
        cmds.modelEditor(modelPanel, edit=1, wos=0)
    else:
        cmds.modelEditor(modelPanel,edit=1,wos=1)

def toggleWireSelected():
    selWire = cmds.displayPref( query=True, wireframeOnShadedActive=True)
    if (selWire == "none"):
        cmds.displayPref(wireframeOnShadedActive= "full")
    else:
        cmds.displayPref(wireframeOnShadedActive= "none")

def toggleXRay():
    modelPanel=cmds.getPanel( withFocus=True )
    if (cmds.modelEditor(modelPanel, query=1, xray=1)==1):
        cmds.modelEditor(modelPanel, edit=1, xray=0)
    else:
        cmds.modelEditor(modelPanel,edit=1,xray=1)




def win_outliner(*args):
    cmds.OutlinerWindow()

    
def win_hypershade(*args):
    cmds.HypershadeWindow()


def win_graphEditor(*args):
    #cmds.GER(l=True)
    cmds.GraphEditor()
    
    
def win_scriptEditor(*args):
    cmds.ScriptEditor()


def win_connectionEditor(*args):
    cmds.ConnectionEditor()
    

def win_nodeEditor(*args):
    try:
	cmds.NodeEditorWindow()    
    except:
	invalid_version_window()
    
def win_modelingToolkit(*args):
    cmds.ToggleModelingToolkit()


    
    


#ActivateUI()
